<template>
  <div class="AppCard">
    <div class="cardA" v-if="type === 1">
      <img :src="imgSrc()" alt="img" width="40px" />
      <div class="details">
        <div class="title">{{ appInfo.app_name }}</div>
        <div class="describe">{{ appInfo.describe }}</div>
      </div>
      <div class="selection">
        <img
          v-if="appInfo.collected"
          src="@/assets/images/collection.svg"
          alt=""
          @click.stop="upSelected(0)"
        />
        <i v-else class="el-icon-star-off" @click.stop="upSelected(1)"></i>
      </div>
    </div>
    <div class="cardB" v-else-if="type === 2">
      <img :src="imgSrc()" alt="img" width="40px" />
      <div class="title">{{ appInfo.app_name }}</div>
      <div class="selection">
        <img
          v-if="appInfo.collected"
          src="@/assets/images/collection.svg"
          alt=""
          @click.stop="upSelected(0)"
        />
        <i v-else class="el-icon-star-off" @click.stop="upSelected(1)"></i>
      </div>
    </div>
    <div class="cardC" v-else>
      <img :src="imgSrc()" alt="img" width="40px" />
      <div class="title">{{ appInfo.app_name }}</div>
      <div class="describe">{{ appInfo.describe }}</div>
      <div class="time">
        <i class="el-icon-time"></i>
        {{ appInfo.update_time }}
      </div>
      <span class="setting">
        <i class="el-icon-setting" @click="openDialog"></i>
      </span>
    </div>
    <el-dialog
      title="编辑应用"
      :visible.sync="dialogVisible"
      width="30%"
      custom-class="app-card-dialog"
    >
      <el-form :model="form" :rules="rules">
        <el-form-item label="应用名称" prop="app_name">
          <el-input
            v-model="form.app_name"
            placeholder="请输入应用名称"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="应用描述" prop="describe">
          <el-input
            v-model="form.describe"
            placeholder="请输入应用描述"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="应用类别" prop="describe">
          <el-select v-model="form.category_id" size="small">
            <el-option
              v-for="item in categories"
              :label="item.category_name_zh"
              :value="item.category_id"
              :key="item.category_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定流程规则">
          <el-input
            v-model="appInfo.process_rule_code"
            disabled
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择图标">
          <div class="icon-row" v-for="ind in 5" :key="`row-${ind}`">
            <span
              v-for="iind in 8"
              :key="`img-${ind}-${iind}`"
              :class="[
                `img-${ind}-${iind}`,
                (ind - 1) * 8 + iind === form.icon_index ? 'active' : '',
              ]"
              @click="form.icon_index = (ind - 1) * 8 + iind"
            >
              <img :src="imgSrcs[(ind - 1) * 8 + iind - 1]" alt="img" />
              <span class="selected">
                <i class="el-icon-check"></i>
              </span>
            </span>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getActiveCategory, updateApp, updateSelected } from "@/http/api";
import dayjs from "dayjs";
export default {
  name: "AppCard",
  props: ["type", "appInfo"],
  emits: ["updateSeleted", "getApps"],
  data() {
    return {
      dialogVisible: false,
      form: {
        app_name: this.appInfo.app_name,
        describe: this.appInfo.describe,
        icon_index: this.appInfo.icon_index,
        category_id: this.appInfo.category_id,
      },
      categories: [],
      rules: {
        app_name: [{ required: true, message: "请输入应用名称" }],
        describe: [{ required: true, message: "请输入应用描述" }],
      },
      imgSrcs: [],
    };
  },
  mounted() {},
  watch: {
    appInfo: {
      handler(newVal) {
        this.appInfo = newVal
        this.appInfo.update_time = dayjs(this.appInfo.update_time).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      },
      immediate: true
    },
  },
  methods: {
    async upSelected(mode) {
      let res = await updateSelected({
        app_id: this.appInfo.app_id,
        mode,
      });
      if (res.code === 20031) {
        this.$emit("updateSeleted", res.data);
      } else {
        this.$message.error(res.msg);
      }
    },
    async openDialog() {
      if (!this.imgSrcs.length) {
        for (let index = 1; index <= 40; index++) {
          this.imgSrcs.push(
            require("../../assets/icons/icon" + index + ".svg")
          );
        }
      }
      let res = await getActiveCategory();
      if (res.code === 20041) {
        this.categories = res.data;
      }
      this.dialogVisible = true;
    },
    async submit() {
      let res = await updateApp({
        app_id: this.appInfo.app_id,
        ...this.form,
      });
      if (res.code === 20031) {
        this.$emit("getApps");
        this.dialogVisible = false;
      }
    },
    imgSrc() {
      return require(`../../assets/icons/icon${this.appInfo.icon_index}.svg`)
    }
  },
};
</script>
<style lang="less" scoped>
.AppCard {
  .cardA,
  .cardB {
    background-color: #fff;
    display: flex;
    align-items: center;
    border: 1px solid #e8eaed;
    border-radius: 4px;
    position: relative;
    .title {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.85);
      margin-top: 4px;
    }
    .selection {
      width: 24px;
      text-align: center;
      flex: 0;
      position: absolute;
      top: 0;
      right: 20px;
      i {
        display: none;
        cursor: pointer;
        margin-top: 10px;
        &:hover {
          color: #ffad14;
        }
      }
    }
    &:hover {
      box-shadow: 0 0 12px rgba(0, 0, 0, 0.08);
      .selection i {
        display: block;
      }
    }
  }
  .cardA {
    width: 210px;
    padding: 16px 0 20px 16px;
    .details {
      padding-left: 10px;
      .title {
        line-height: 16px;
        padding-bottom: 4px;
      }
      .describe {
        font-size: 12px;
        color: #8f959e;
        line-height: 12px;
      }
    }
  }
  .cardB {
    flex-direction: column;
    padding: 10px 30px;
    width: 120px;
    height: 100px;
    .title {
      text-align: center;
    }
    .selection {
      right: 10px;
    }
  }
  .cardC {
    overflow: hidden;
    padding: 20px 18px;
    background-color: #fff;
    border-radius: 8px;
    cursor: pointer;
    position: relative;
    &:hover {
      box-shadow: 0 8px 16px 0 rgba(20, 87, 183, 0.11);
    }
    .title {
      color: #1f2329;
      font-size: 16px;
      margin: 8px 0 4px;
    }
    .describe {
      color: #2b2f36;
      font-size: 14px;
      margin-bottom: 8px;
    }
    .time {
      color: #8f959e;
      font-size: 12px;
    }
    .setting {
      position: absolute;
      right: 20px;
      bottom: 16px;
      padding: 4px;
      border-radius: 50%;
      width: 26px;
      height: 26px;
      display: flex;
      align-items: center;
      justify-content: center;
      &:hover {
        background-color: #ebf0fc;
        i {
          color: #4c78fc;
        }
      }
      i {
        color: #bbbfc4;
      }
    }
  }
}
</style>
<style lang="less">
.app-card-dialog {
  border-radius: 6px;
  overflow: hidden;
  .el-dialog__header {
    background: #4c78fc;
    padding: 12px 24px;
    .el-dialog__title {
      color: #fff;
      font-size: 16px;
    }
    .el-dialog__headerbtn {
      top: 16px;
      .el-dialog__close {
        color: #fff;
      }
    }
  }
  .el-dialog__body {
    padding: 24px;
    .el-form-item__label {
      line-height: 30px;
    }
    .el-form-item__content {
      line-height: 10px;
    }
    .icon-row {
      display: flex;
      width: 100%;
      & > span {
        flex: 1;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
        img {
          width: 100%;
        }
        &:last-child {
          margin-right: 0;
        }
        span {
          display: none;
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background-color: rgba(0, 0, 0, 0.4);
          border-radius: 12px;
          color: #fff;
          justify-content: center;
          align-items: center;
        }
        &.active span {
          display: flex;
        }
      }
      &:first-child {
        margin-top: 30px;
      }
    }
  }
}
</style>
